#!/usr/bin/env python
# -*- coding: utf8 -*-
# title       :
# description :
# author      :'ShenMeng'


from PySide import QtCore, QtGui
import os
from .pyuic.list_view_list_widget import Ui_list_view_list_widget
from .list_view_delegate import ListViewWidget, ListViewDelegate

class ListViewListWidget(ListViewWidget):
    def __init__(self, parent):
        ListViewWidget.__init__(self, Ui_list_view_list_widget, parent)

    def set_text(self, header, body):
        self.setToolTip("%s<br>%s" % (header, body))
        self.ui.header_label.setText(header)
        self.ui.body_label.setText(body)

    @staticmethod
    def calculate_size():
        return QtCore.QSize(200, 90)


class ListViewListDelegate(ListViewDelegate):
    def _create_widget(self, parent):
        """
        Widget factory as required by base class. The base class will call this
        when a widget is needed and then pass this widget in to the various callbacks.
        :param parent: Parent object for the widget
        """
        return ListViewListWidget(parent)
    
    def _on_before_paint(self, widget, model_index, style_options):
        is_selected = self._view.selectionModel().isSelected(model_index)
        widget.set_selected(is_selected)
        item = self._view.model().itemFromIndex(model_index)
        data = item.data(3)
        if is_selected and len(self._view.selectionModel().selectedIndexes()) == 1:
            widget.set_button_visible(True)
        else:
            widget.set_button_visible(False)
        if data:
            header_str = ""
            header_str += "<b style='color:#2C93E2'>Version %s</b>" % data.version
            body_str = ''# "<i>%s</i>: %s<br>" % ('', '')
            icon = QtGui.QPixmap(os.path.splitext(data.path)[0] + '.png')
            width = self._view.iconSize().width()
            height = self._view.iconSize().height()
            # thumb = icon.scaled(QtCore.QSize(width, height), QtCore.Qt.KeepAspectRatio,
            #                     QtCore.Qt.SmoothTransformation)
            widget.set_thumbnail(icon)
            widget.set_text(header_str, body_str)

    def sizeHint(self, style_options, model_index):
        return ListViewListWidget.calculate_size()
             
